package baodian;

import org.junit.Test;

import java.util.LinkedHashSet;

/**
 * @author fance
 * @date 18-4-17 下午3:59
 */
public class StringSum {

    // 逆序 reverse();

    // 两个字符串是否由相同字符组成 1.排序 比较 2. int[256] 保存串1中各字符出现次数,串2 依次减减.判断数组
    // 中是否全为0

    /**
     * 统计单词个数
     * @param s
     * @return
     */
    public int wordCnt(String s) {
        int word = 0;
        int cnt = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                word = 0;
            } else if (word == 0) {
                word = 1;
                cnt++;
            }
        }
        return cnt;
    }

    /**
     * 删除重复字符
     * @param s
     * @return
     */
    public String removeDupChar(String s) {
        LinkedHashSet<Character> set = new LinkedHashSet<>();
        for (char c : s.toCharArray()) {
            set.add(c);
        }
        StringBuilder sb = new StringBuilder();
        for (Character c: set
             ) {
            sb.append(c);
        }
        return sb.toString();
    }


    @Test
    public void test() {
        System.out.println(removeDupChar("good"));
    }

}
